﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExpertMIS.Entities;
using System.Text;
using System.Collections;

namespace ExpertMIS.UserControl
{
    public partial class ucExpertSlider : System.Web.UI.UserControl
    {
        private IList<ExpertEntity> _expertList = null;
        private bool firstOne = false;
        private string visibledOne = string.Empty;
        private Hashtable htMapping = new Hashtable();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected string GetHeader()
        {
            StringBuilder sb = new StringBuilder();
            firstOne = true;
            foreach (ExpertEntity expert in _expertList)
            {
                if (sb.Length > 0) sb.Append("|");
                string controlId = htMapping[expert] as string;
                sb.Append(string.Format("<a href='#' onclick='{2}'><span {1}>{0}</span></a>", expert.Name, firstOne ? "class='expertslider_choosed'" : "class='expertslider_unchoosed'", "do" + ClientID + "Switch(\"" + controlId + "\");"));
                firstOne = false;
            }
            return sb.ToString();
        }

        public void LoadExpertList(IList<ExpertEntity> list)
        {
            firstOne = true;
            _expertList = list;
            repExperts.DataSource = list;
            repExperts.DataBind();
        }

        protected void repExperts_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ucExpertView ucExpertView1 = e.Item.FindControl("ucExpertView1") as ucExpertView;
                if (ucExpertView1 != null)
                {
                    ucExpertView1.LoadExpert(e.Item.DataItem as ExpertEntity);
                    ucExpertView1.SetVisible(firstOne);
                    if (firstOne)
                    {
                        visibledOne = ucExpertView1.ClientID;
                    }
                    firstOne = false;

                    htMapping.Add(e.Item.DataItem, ucExpertView1.ClientID);
                }
            }
        }

        protected string GetRegisterVariable()
        {
            return string.Format("var {0}_chooseOne = '{1}';", ClientID, visibledOne);
        }

        protected string GetVaiableName()
        {
            return string.Format("{0}_chooseOne", ClientID);
        }
    }
}